package com.speed.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.Ret;
import com.speed.entity.Activiter;
import com.speed.entity.MenberCardStartactBm;

public class MenberCardStarctBmService {

	public void saveMenberCard(Ret r) {

		MenberCardStartactBm bm = MenberCardStartactBm.MODEL_DAO.findFirst(
				"select * from " + MenberCardStartactBm.TABLE_NAME + " where " + MenberCardStartactBm.MP + " = ? ",
				r.get(MenberCardStartactBm.MP));
		if (bm != null) {
			return;
		}
		// 报名活动ID
		Integer recordId = r.get(MenberCardStartactBm.RECORD_ID);
		// 报名人手机
		String mp = r.get(MenberCardStartactBm.MP);
		// 报名人姓名
		String name = r.get(MenberCardStartactBm.NAME);
		// 入口网站
		String from = r.get(MenberCardStartactBm.FROM);
		// 报名序号F
		String ordernum = r.get(MenberCardStartactBm.ORDERNUM);

		new MenberCardStartactBm().set(MenberCardStartactBm.NAME, name).set(MenberCardStartactBm.MP, mp)
				.set(MenberCardStartactBm.FROM, from).set(MenberCardStartactBm.RECORD_ID, recordId)
				.set(MenberCardStartactBm.ORDERNUM, ordernum).save();
	}

	
	public MenberCardStartactBm findMenberCardStarctBmForOne(Ret ret,List<String> whereCodis,String... fields){
		StringBuffer sql = new StringBuffer("select ");
		if (fields != null || fields.length != 0) {
			for (int i = 0 ; i <fields.length;i++) {
				sql.append(fields[i]);
				if(i != fields.length - 1){
					sql.append(",");
				}
			}
		}else{
			sql.append("*");
		}
		sql.append(" from " +MenberCardStartactBm.TABLE_NAME);
		
		if(ret == null || whereCodis == null || whereCodis.isEmpty()){
			return MenberCardStartactBm.MODEL_DAO.findFirst(sql.toString());
		}
		List<Object> paras = new ArrayList<Object>();
		sql.append(" where 1=1 ");
		for(String codi: whereCodis){
			sql.append(" and "+codi +" = ? ");
			paras.add(ret.get(codi));
		}
		return MenberCardStartactBm.MODEL_DAO.findFirst(sql.toString(),paras.toArray());
	}
	public List<MenberCardStartactBm> findMenberCardStarctBmForMore(Ret ret,List<String> whereCodis,String... fields){
		StringBuffer sql = new StringBuffer("select ");
		if (fields != null || fields.length != 0) {
			for (int i = 0 ; i <fields.length;i++) {
				sql.append(fields[i]);
				if(i != fields.length - 1){
					sql.append(",");
				}
			}
		}else{
			sql.append("*");
		}
		sql.append(" from " +MenberCardStartactBm.TABLE_NAME);
		
		if(ret == null || whereCodis == null || whereCodis.isEmpty()){
			return MenberCardStartactBm.MODEL_DAO.find(sql.toString());
		}
		List<Object> paras = new ArrayList<Object>();
		sql.append(" where 1=1 ");
		for(String codi: whereCodis){
			sql.append(" and "+codi +" = ? ");
			paras.add(ret.get(codi));
		}
		return MenberCardStartactBm.MODEL_DAO.find(sql.toString(),paras.toArray());
	}
	
}
